package jlxy.ise.eroad.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;
import jlxy.ise.eroad.R;
import jlxy.ise.eroad.base.BaseSwitchFragment;
import jlxy.ise.eroad.bean.ServerRpsInfo;
import jlxy.ise.eroad.util.SingleToast;
import jlxy.ise.eroad.util.WebUtil;


/**
 * 修改密码页面
 *
 * @author chen
 */
public class ModifyPwdFragment extends BaseSwitchFragment {

//	private static final String TAG = ModifyPwdFragment.class.getSimpleName();

    private View rootView;

    private EditText mEdNew;
    private EditText mEdNewR;
    private Button mBtnModify;

    private static ModifyPwdFragment instance;

    public static ModifyPwdFragment getInstance() {
        if (instance == null) instance = new ModifyPwdFragment();
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_modify_pwd, container, false);

        mEdNew = (EditText) rootView.findViewById(R.id.et_pwd_new);
        mEdNewR = (EditText) rootView.findViewById(R.id.et_pwd_new_repeat);
        mBtnModify = (Button) rootView.findViewById(R.id.btn_pwd_modify);

        mBtnModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newPwd = mEdNew.getEditableText().toString();
                String newPwdR = mEdNewR.getEditableText().toString();
                if (!newPwd.equals(newPwdR)) {
                    SingleToast.showToast(mAct, "两次新密码不一致", 2000);
                } else {
                    //修改密码
                    WebUtil.postDriverResetVerify(WebUtil.getRpDriverResetVerify(mApp.getDriverInfo().getId() + "", newPwd), new JsonHttpResponseHandler() {

                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            super.onSuccess(statusCode, headers, response);
                            Gson gson = new Gson();
                            try {
                                ServerRpsInfo serverRpsInfo = gson.fromJson(response.toString(), ServerRpsInfo.class);
                                if (serverRpsInfo.getStatus().equals("ok") && serverRpsInfo.getMessage().equals("ok")) {
                                    SingleToast.showToast(mAct, "修改密码成功", 2000);
                                }
                            } catch (Exception e) {
                                SingleToast.showToast(mAct, "修改密码出错:" + e.getLocalizedMessage(), 2000);
                            }
                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                            super.onFailure(statusCode, headers, throwable, errorResponse);
                            SingleToast.showToast(mAct, "修改密码失败", 2000);
                        }
                    });
                }
            }
        });

        return rootView;
    }

}
